package com.ktjiaoyu.crm.service.impl;

import com.ktjiaoyu.crm.entity.Chance;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.ChanceRepository;
import com.ktjiaoyu.crm.repository.UserRepository;
import com.ktjiaoyu.crm.service.IChanceService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service(value = "chanceService")
public class ChanceServiceImpl implements IChanceService {
    @Resource
    private ChanceRepository chanceRepository;

    @Resource
    private UserRepository userRepository;
    @Override
    public void save(Chance chance) {
        this.chanceRepository.save(chance);
    }

    @Override
    public void delete(Long chcId) {
        Chance chance = chanceRepository.getOne(chcId);
        chanceRepository.delete(chance);
    }

    @Override
    public Chance getChance(Long chcId) {
        return chanceRepository.getOne(chcId);
    }



    @Override
    public Page<Chance> findAllChance(String chcCustName, String chcTitle,
                                      String chcLinkman, String chcStatus,
                                      Long chcCreateId, Long chcDueId, Pageable pageable) {
        Specification<Chance> chanceSpecification = new Specification<Chance>() {
            @Override
            public Predicate toPredicate(Root<Chance> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (chcCustName!=null && ! chcCustName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcCustName"), "%"+chcCustName+"%"));
                }
                if (chcTitle!=null && !chcTitle.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("chcTitle"), "%"+chcTitle+"%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return chanceRepository.findAll(chanceSpecification, pageable);
    }


    @Override
    public void edit(Chance chance) {
        this.chanceRepository.edit(chance);
    }

    @Override
    public void updateChcStatusByChcId(String chcStatus, Long chcId) {
        this.chanceRepository.updateChcStatusByChcId(chcStatus, chcId);
    }

    @Override
    public List<User> findUsersByRoleId(Long roleId) {
        return userRepository.findUsersByRoleRoleId(roleId);
    }

    @Override
    public Page<Chance> findAllChance(String chcCustName, String chcTitle, Pageable pageable) {
        return null;
    }
}
